﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;

namespace homework_8
{
    class orderservice
    {
        private List<order> orderList = new List<order>();

        //添加订单
        public void AddOrder(order order)
        {
            if (order == null)
                throw new ArgumentException("订单不能为空");
            if (orderList.Contains(order))
                throw new ArgumentException("不能存在重复订单");
            orderList.Add(order);
        }

        //删除订单
        public static void DeleteOrder(int ordernum)
        {
            orderList.RemoveAll(o => o.ordernum == ordernum);
        }

        //修改订单
        public static void ModifyOrder(order order)
        {
            if (order == null)
                throw new ArgumentException("不能存在空订单");


        }


        //查询订单（按照订单号、商品名称、客户、订单金额等进行查询）
        public List<order> QueryByName(string Name)
        {
            var query = orderList.Where(
              o => o.Details.Any(d => d.ProductName == Name));
            return query.ToList();
        }

        public List<order> QueryBySum(float sum)
        {
            var query = orderList.Where(o => o.sum >= sum);
            return query.ToList();
        }

        public List<order> QueryByBuyer(string Buyer)
        {
            var query = orderList
                .Where(o => o.buyer == Buyer);
            return query.ToList();
        }


        public orderservice()
        {
        }


        public void Export(String fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<order>));
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                xs.Serialize(fs, orderList);
            }
        }

        public void Import(string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<order>));
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                List<order> temp = (List<order>)xs.Deserialize(fs);
                temp.ForEach(order => {
                    if (!orderList.Contains(order))
                    {
                        orderList.Add(order);
                    }
                });
            }
        }
    }
}
